package com.pillpoppers.pillbox;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

public class AlarmReceiver extends BroadcastReceiver {

	@Override
	public void onReceive(Context context, Intent intent) {
		Intent alarmIntent = new Intent(context, AlarmExecutionActivity.class);
		alarmIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		int scheduleId = intent.getIntExtra("scheduleId", 0);
		alarmIntent.putExtra("scheduleId", scheduleId);
		alarmIntent.putExtra("alarmBy", intent.getIntExtra("alarmBy", Schedule.ALARM_BY_SILENT));
		alarmIntent.putExtra("pillIds", intent.getIntegerArrayListExtra("pillIds"));
		context.startActivity(alarmIntent);
		
		PendingIntent resultPendingIntent = PendingIntent.getActivity(context, scheduleId, new Intent(context, MainFragmentActivity.class), 0);
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
		notificationManager.notify(scheduleId, buildNotification(context, resultPendingIntent));
		// TODO needs to go to schedule page with this time schedule
	}
	
	private Notification buildNotification(Context context, PendingIntent pendingIntent) {
		return new NotificationCompat.Builder(context)
				.setSmallIcon(R.drawable.ic_launcher)
				.setContentTitle("Time to take your pill")
				.setContentText("Touch for detail")
				.setContentIntent(pendingIntent)
				.setAutoCancel(true)
				.build();
	}

}
